
//  Copyright (c) Microsoft Corporation.  All Rights Reserved.

using System;
using System.ServiceModel;
using service;
using System.Collections.Generic;

namespace Microsoft.ServiceModel.Samples
{
    //The service contract is defined in generatedClient.cs, generated from the service by the svcutil tool.

    //Client implementation code.
    class Client
    {
        static void Main()
        {
            System.Threading.Thread.Sleep(300);
            
            // Create a client with given client endpoint configuration
            CalculatorClient client = null;
            /*
             client = new CalculatorClient();
                        // Call the Add service operation.
                        double value1 = 100.00D;
                        double value2 = 15.99D;
                        double result = client.Add(value1, value2);
                        Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

                        // Call the Subtract service operation.
                        value1 = 145.00D;
                        value2 = 76.54D;
                        result = client.Subtract(value1, value2);
                        Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

                        // Call the Multiply service operation.
                        value1 = 9.00D;
                        value2 = 81.25D;
                        result = client.Multiply(value1, value2);
                        Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

                        // Call the Divide service operation.
                        value1 = 22.00D;
                        value2 = 7.00D;
                        result = client.Divide(value1, value2);
                        Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
                        client.Close();
                        */

            Dictionary<string, PokerTable>.KeyCollection keys;

            string id=null;

            int cmd = -1;
            string name = null;
            while (cmd != 0)
            {
                switch (cmd)
                {
                    case 1:
                        {
                            if (name == null)
                            {
                                Console.Write("\nyour name is : ");
                                name = Console.ReadLine();
                                Console.WriteLine("Hello " + name + " !");
                            }
                            break;
                        }
                    case 2:
                        {
                            client = null;
                            try
                            {
                                client = new CalculatorClient();
                                client.Open();
                                Console.WriteLine("Create Table result : " + client.createTable());
                                client.Close();
                            }
                            catch
                            {
                                if (client != null && client.State == CommunicationState.Opened)
                                    client.Close();
                                Console.WriteLine("\nERROR !!!!!!!!!!!!!!!!!!!!!! 2\n");
                            }
                            break;
                        }
                    case 3:
                        {
                            if (name != null)
                            {
                                client = null;
                                try
                                {
                                    client = new CalculatorClient();
                                    client.Open();
                                    int i = 0;
                                    keys = client.getTables();
                                    foreach (string s in keys)
                                    {
                                        
                                        Console.WriteLine(i + ". table id : " + s);
                                        i++;
                                    }
                                    client.Close();

                                    Console.Write("\nJoin table : ");
                                    int tbl = -1;
                                    if (int.TryParse(Console.ReadLine(),out tbl) && tbl < keys.Count)
                                    {
                                        i = 0;
                                        foreach (string s in keys)
                                        {
                                            
                                            if (i == tbl)
                                            {
                                                id = s;
                                                break;
                                            }
                                            i++;
                                        }
                                        try
                                        {
                                            client = new CalculatorClient();
                                            client.Open();
                                            Console.WriteLine("client.joinTable(" + id + ", " + name + ")=" + client.joinTable(id, name));
                                            client.Close();
                                        }
                                        catch
                                        {
                                            if (client != null && client.State == CommunicationState.Opened)
                                                client.Close();
                                            Console.WriteLine("\nERROR !!!!!!!!!!!!!!!!!!!!!! 3a\n");
                                        }
                                    }
                                }
                                catch
                                {
                                    if (client != null && client.State == CommunicationState.Opened)
                                        client.Close();
                                    Console.WriteLine("\nERROR !!!!!!!!!!!!!!!!!!!!!! 3b\n");
                                }
                            }
                            break;
                        }
                    case 4:
                        {
                            int bet = -1;
                            Console.WriteLine("Your Bet : ");
                            if (name != null && id != null && int.TryParse(Console.ReadLine(),out bet))
                            {
                                client = null;
                                try
                                {
                                    client = new CalculatorClient();
                                    client.Open();
                                    Console.WriteLine("bet "+bet+" result : " + client.bet(id,name,bet));
                                    Console.WriteLine(client.getCurrentBets(id));
                                    client.Close();
                                }
                                catch
                                {
                                    if (client != null && client.State == CommunicationState.Opened)
                                        client.Close();
                                    Console.WriteLine("\nERROR !!!!!!!!!!!!!!!!!!!!!! 4\n");
                                }
                            }
                            break;
                        }
                    case 5:
                        {
                            client = null;
                            try
                            {
                                client = new CalculatorClient();
                                client.Open();
                                client.Sleep();
                                client.Close();
                            }
                            catch
                            {
                                if (client != null && client.State == CommunicationState.Opened)
                                    client.Close();
                                Console.WriteLine("\nERROR !!!!!!!!!!!!!!!!!!!!!! 5\n");
                            }
                            break;
                        }
                }
                Console.WriteLine("1. Your Name");
                Console.WriteLine("2. Create Table");
                Console.WriteLine("3. Join Table");
                Console.WriteLine("4. Bet");
                Console.WriteLine("5. Sleep(10000)");
                Console.WriteLine();
                Console.WriteLine("0. Exit");
                Console.WriteLine(" ----------------------------------------");
                int.TryParse(Console.ReadLine(), out cmd);
            }

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
}
